<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="任务名称" prop="jobName">
          <el-input
            v-model="queryParams.jobName"
            placeholder="请输入任务名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
       
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_job_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitor:job:add']"
        >新增</el-button>
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['monitor:job:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['monitor:job:remove']"
          >删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['monitor:job:export']"
          >导出</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-s-operation"
            size="mini"
            @click="handleJobLog"
            v-hasPermi="['monitor:job:query']"
          >日志</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="jobList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="expand">
      <template slot-scope="scope">
            <!-- <span>{{ scope.row.jobId }}</span> -->
            <el-table class="table2" v-loading="loading" :data="scope.row.tasks" >
              <el-table-column label="" width="48" align="center"  >
                        <template slot="header"><i class="" style=" font-size: 20px;"></i></template>
                </el-table-column>
                <el-table-column label="" width="55" align="center"  >
                        <template slot="header"><i class="el-icon-caret-right" style=" font-size: 20px;"></i></template>
                </el-table-column>
                <el-table-column label="" width="55" align="center"  >
                        <template slot="header"><i class="" style=" font-size: 20px;"></i></template>
                </el-table-column>
                <el-table-column label="用户名"  width="120" align="center" prop="user.userName" />
                <el-table-column label="用户ID" align="center" prop="user.userId" />
                <el-table-column label="标题" align="center" prop="title" />
                <el-table-column label="内容" align="center" prop="content" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateTask(scope.row)"
            v-hasPermi="['system:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteTask(scope.row)"
            v-hasPermi="['system:task:remove']"
          >删除</el-button>
        </template>
        </el-table-column>
        </el-table>
        </template>
        </el-table-column>
        <el-table-column label="Id" width="55" align="center" prop="jobId" />
        <el-table-column label="任务名称" width="120" align="center" prop="jobName" :show-overflow-tooltip="true" />
        <el-table-column label="描述" align="center" prop="expression" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="cron执行表达式" align="center" prop="cronExpression" :show-overflow-tooltip="true" /> -->
        <el-table-column label="更新时间" align="center" prop="updateTime" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s} ') }}</span>
        </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              width = "50"
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['monitor:job:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['monitor:job:remove']"
            >删除</el-button>
            <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['monitor:job:changeStatus', 'monitor:job:query']">
              <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="addTask" icon="el-icon-circle-plus">添加</el-dropdown-item>
                <el-dropdown-item command="handleRun" icon="el-icon-caret-right"
                  v-hasPermi="['monitor:job:changeStatus']">执行一次</el-dropdown-item>
                <el-dropdown-item command="handleView" icon="el-icon-view"
                  v-hasPermi="['monitor:job:query']">任务详细</el-dropdown-item>
                <el-dropdown-item command="handleJobLog" icon="el-icon-s-operation"
                  v-hasPermi="['monitor:job:query']">调度日志</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <!-- 添加或修改定时子任务对话框 -->
      <el-dialog :title="tasktitle" :visible.sync="openTask" width="500px" append-to-body>
      <el-form ref="taskform" :model="taskform" :rules="taskformRules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="taskform.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="taskform.content" :min-height="192"/>
        </el-form-item>
        <!-- <el-form-item label="模板id" prop="modelId">
          <el-input v-model="taskform.modelId" placeholder="请输入模板id" />
        </el-form-item> -->
        <el-form-item label="用户id" prop="userId">
          <el-select v-model="taskform.userId" placeholder="请选择用户">
          <el-option
            v-for="item in users"
            :key="item.key"
            :label="item.label"
            :value="item.key"
            :disabled="item.disabled">
          </el-option>
        </el-select>
          <!-- <el-input v-model="taskform.userId" placeholder="请输入用户id" /> -->
        </el-form-item>
       
        <el-form-item label="父任务id" prop="jobId">
          <el-input v-model="taskform.jobId" :disabled="true"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTaskForm">确 定</el-button>
        <el-button @click="cancelTask">取 消</el-button>
      </div>
    </el-dialog>
<!-- 父任务添加 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
        <div v-if="title=='修改任务'">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="任务名称" prop="jobName">
                <el-input v-model="form.jobName" placeholder="请输入任务名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="描述" prop="expression">
                <el-input v-model="form.expression" placeholder="请输入描述" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="cron表达式" prop="cronExpression">
                <el-input v-model="form.cronExpression" placeholder="请输入cron执行表达式">
                  <template slot="append">
                    <el-button type="primary" @click="handleShowCron">
                      生成表达式
                      <i class="el-icon-time el-icon--right"></i>
                    </el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="执行策略" prop="misfirePolicy">
                <el-radio-group v-model="form.misfirePolicy" size="small">
                  <el-radio-button label="1">立即执行</el-radio-button>
                  <el-radio-button label="2">执行一次</el-radio-button>
                  <el-radio-button label="3">放弃执行</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否并发" prop="concurrent">
                <el-radio-group v-model="form.concurrent" size="small">
                  <el-radio-button label="0">允许</el-radio-button>
                  <el-radio-button label="1">禁止</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_job_status"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>


      <div v-else style="height: auto;">

    <el-steps :active="active" finish-status="success">
        <el-step title="第一步" />
        <el-step title="第二步" />
        <el-step title="第三步" />
    </el-steps>
    
    <!-- 1.任务设置 -->
    <div v-show="active == 0">
      <el-main>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="任务名称" prop="jobName">
                <el-input v-model="form.jobName" placeholder="请输入任务名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="描述" prop="expression">
                <el-input v-model="form.expression" placeholder="请输入描述" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="cron表达式" prop="cronExpression">
                <el-input v-model="form.cronExpression" placeholder="请输入cron执行表达式">
                  <template slot="append">
                    <el-button type="primary" @click="handleShowCron">
                      生成表达式
                      <i class="el-icon-time el-icon--right"></i>
                    </el-button>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="执行策略" prop="misfirePolicy">
                <el-radio-group v-model="form.misfirePolicy" size="small">
                  <el-radio-button label="1">立即执行</el-radio-button>
                  <el-radio-button label="2">执行一次</el-radio-button>
                  <el-radio-button label="3">放弃执行</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否并发" prop="concurrent">
                <el-radio-group v-model="form.concurrent" size="small">
                  <el-radio-button label="0">允许</el-radio-button>
                  <el-radio-button label="1">禁止</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_job_status"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-main>
      <el-footer style="">
        <div style="float: right;">
          <el-button v-if="active > 0" style="margin-top: 12px" @click="pre">上一步</el-button>
          <el-button v-if="active < 2" style="margin-top: 12px" @click="next">下一步</el-button>
        </div>
      </el-footer>
      
    </div>

       <!-- 2.用户选择 -->
    <div v-show="active == 1">
      <el-main v-if="active == 1">
        <user-table @getUserIds="getUserIds(arguments)"></user-table>

      </el-main>
      <el-footer>
        <div style="float: right;">
          <el-button v-if="active > 0" style="margin-top: 12px" @click="pre">上一步</el-button>
          <el-button v-if="active < 2" style="margin-top: 12px" @click="next">下一步</el-button>
        </div>
      </el-footer>
    </div>


        <!--3.内容填写  -->
    <div v-show="active == 2" style="margin-top: 2%;">
      <div style="padding-left: 20px;">
        <el-radio style="margin-left: 14px;" v-model="radio1" label="1" @change="changeCon" border>自定义内容</el-radio>
        <el-radio style="margin-left: 11px;" v-model="radio1" label="2" @change="changeCon" border>选择现有模板</el-radio>
        </div>
      <el-main v-if="radio1 == 1">
        
            <el-form class="defineself"  ref="form2" :model="form2" :rules="model_rules" label-width="80px">
            <el-form-item  label="模板标题" prop="title">
              <el-input v-model="form2.title" placeholder="请输入标题" />
            </el-form-item>
            <el-form-item label="模板内容" prop="content" style="color: #303133;font-size:13px;font-weight: bold;">
              <editor v-model="form2.content" :min-height="192"/>
            </el-form-item>
            <el-form-item label="模板类型" prop="type">
            <el-input v-model="form2.type" clearable placeholder="请选择信息类型">
            </el-input>
          </el-form-item>
          <el-form-item label="发送方式" prop="type">
          <el-radio v-model="radio" label="邮件">邮件</el-radio>
           <el-radio v-model="radio" label="短信" disabled>短信</el-radio>
          </el-form-item>
          </el-form>

      </el-main>
      <el-main v-if="radio1 == 2">
          <el-form class=""   :rules="model_rules" label-width="80px">
            <el-form-item  label="选择模板" prop="">
              <el-select
                v-model="value2" placeholder="请选择" @change="sellectTotal">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled">
                </el-option>
             </el-select>
            </el-form-item>
      <el-form-item  label="模板内容预览" >
        <!-- <span style="margin-left: 10px;font-size: 18px;font-weight: bold; ">预览</span> -->
      <!-- <div style=";width:95%;height: 100px;margin-left: 10px;margin-top:10px "> -->
        <el-input type="textarea" :rows="4" v-model="value2" disabled :min-height="192"></el-input>
        <!-- <p style="font-size:15px;font-weight: lighter;color: black;margin-top: 0px;">{{ value2 }}</p> -->
      <!-- </div> -->
      </el-form-item>
     
      <el-form-item label="发送方式" prop="type">
      <el-radio v-model="radio" label="邮件">邮件</el-radio>
      <el-radio v-model="radio" label="短信" disabled>短信</el-radio>
      </el-form-item>
  </el-form>
  </el-main>
      <el-footer>
        <div style="float: right;margin-top: 12px">
            <el-button v-if="active > 0"  @click="pre">上一步</el-button>
            <el-button v-if="active < 2"  @click="next">下一步</el-button>
            <el-button type="primary" @click="submitTotal">提 交</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>

      </el-footer>
    </div>
      </div>

      </el-dialog>
  
      <el-dialog title="Cron表达式生成器" :visible.sync="openCron" append-to-body destroy-on-close class="scrollbar">
        <crontab @hide="openCron=false" @fill="crontabFill" :expression="expression"></crontab>
      </el-dialog>
  
      <!-- 任务日志详细 -->
      <el-dialog title="任务详细" :visible.sync="openView" width="700px" append-to-body>
        <el-form ref="form" :model="form" label-width="120px" size="mini">
          <el-row>
            <el-col :span="12">
              <el-form-item label="任务编号：">{{ form.jobId }}</el-form-item>
              <el-form-item label="任务名称：">{{ form.jobName }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务分组：">{{ jobGroupFormat(form) }}</el-form-item>
              <el-form-item label="创建时间：">{{ form.createTime }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务描述：">{{ form.expression }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="cron表达式：">{{ form.cronExpression }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="下次执行时间：">{{ parseTime(form.nextValidTime) }}</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="调用目标方法：">{{ form.invokeTarget }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务状态：">
                <div v-if="form.status == 0">正常</div>
                <div v-else-if="form.status == 1">失败</div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否并发：">
                <div v-if="form.concurrent == 0">允许</div>
                <div v-else-if="form.concurrent == 1">禁止</div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="执行策略：">
                <div v-if="form.misfirePolicy == 0">默认策略</div>
                <div v-else-if="form.misfirePolicy == 1">立即执行</div>
                <div v-else-if="form.misfirePolicy == 2">执行一次</div>
                <div v-else-if="form.misfirePolicy == 3">放弃执行</div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { getJob, addJob, updateJob, runJob, changeJobStatus } from "@/api/monitor/job";
  import {countRun,delJob,listJobByUUID,autoSendAdd, count, lineChartDefault,send,sendBatch} from "@/api/sender/sender";
  import { listTask, getTask, delTask, addTask, updateTask } from "@/api/task/task";
  import Crontab from '@/components/Crontab'
  import userTable from './user.vue'
  import { listModel, getModel, delModel, addModel, updateModel } from "@/api/model/model";
  export default {
    components: { Crontab,userTable},
    props:["users"],
    name: "Job",
    dicts: ['sys_job_group', 'sys_job_status'],
    data() {
      return {
        underRun:0,
        jobVo:{
        jobId:null,
        jobName:null,
        cronExpression:null,
        expression:null,
        // misfirePolicy:"0",
        concurrent:null,//并发（0允许 1禁止）
        status:null,
        userIds:[],
        msg:{}
      },
      msg_id :  null,
      msg_title:null,
        radio:'邮件',
        value2:'',
        radio1: '1',
        msg_form:{},
        form2:{},
        userIds:[],
        active: 0,
        user:'',
        userList:[],
        options: [],
        ops:{
          value:null,
          label:null,
          disabled: false
        },
        openTask:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 定时任务表格数据
        jobList: [],
        // 弹出层标题
        tasktitle: "",
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示详细弹出层
        openView: false,
        // 是否显示Cron表达式弹出层
        openCron: false,
        // 传入的表达式
        expression: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          jobName: undefined,
          jobGroup: undefined,
          status: undefined
        },
        // 表单参数
        taskform: {},
        form: {},
        queryParams_model: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        type: null
      },
        // 表单校验
        rules: {
          jobName: [
            { required: true, message: "任务名称不能为空", trigger: "blur" }
          ],
           expression: [
            { required: true, message: "描述不能为空", trigger: "blur" }
          ],
          // invokeTarget: [
          //   { required: true, message: "调用目标字符串不能为空", trigger: "blur" }
          // ],
          cronExpression: [
            { required: true, message: "cron执行表达式不能为空", trigger: "blur" }
          ],

        },
        model_rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "信息类型", trigger: "blur" }
        ],
        },
        taskformRules:{
          title:[{
            required: true, message: "标题不能为空", trigger: "blur"
          }],
          content: [
            { required: true, message: "内容不能为空", trigger: "blur" }
          ],
          userId: [
            { required: true, message: "请选择用户", trigger: "blur" }
          ],
        },
      };
    },
    created() {
      this.getList();
      this.getListModel();
    },
    methods: {
      countRun(){
      countRun().then(response=>{
        console.log(response)
        this.underRun = response.data
        this.$emit('total',this.underRun)
      })

    },
      //  jobId:null,
        // jobName:null,
        // cronExpression:null,
        // expression:null,
        // // misfirePolicy:"0",
        // concurrent:null,//并发（0允许 1禁止）
        // status:null,
        // userIds:[],
        // msg:{}
      submitTotal(){
        // console.log("----------------")
        //form定时父任务
        // console.log("定时")
          // console.log(this.form)

          this.jobVo.jobName = this.form.jobName
          this.jobVo.cronExpression = this.form.cronExpression
          this.jobVo.expression = this.form.expression
          this.jobVo.misfirePolicy = this.form.misfirePolicy
          this.jobVo.status = this.form.status
          this.jobVo.concurrent = this.form.concurrent
          //用户id列表
          // console.log("用户id列表")
          // console.log(this.userIds)
          this.jobVo.userIds = this.userIds
          // console.log("内容")
          if(this.radio1 == '1'){
            // if(this.form2.type == null || this.form2.title == null || this.form2.content==null){
            //   this.$message.warning('请填写内容');
            //    return
            // }else{
              this.$refs["form2"].validate(valid => {
                if (valid) {
                  if( this.form2.title === null || this.form2.content === null){
                this.$message.warning('内容不能为空');
                  }else{
                    console.log(this.form2)
                            this.jobVo.msg.rank =  this.form2.type;
                            this.jobVo.msg.title = this.form2.title
                            this.jobVo.msg.content = this.form2.content
                            this.jobVo.msg.type = this.radio
                            console.log(this.jobVo)
                            autoSendAdd(this.jobVo).then(response=>{
                if(response.msg=="操作成功"){
                  this.$message.success("定时发送设置成功");
                  this.open = false;
                  setTimeout(200)
                  this.active = 0;
                    this.userIds = null,
                    this.getList()
                    this.changeCon()
                }
                })
                  }
              }
            })

          }else{
            if(this.value2 === null ){
        this.$message.warning('请选择模板');
        return
           }else{
            this.jobVo.msg.title = this.msg_title
            this.jobVo.msg.content =this.value2;
             this.jobVo.msg.modelId = this.msg_id; 
             this.jobVo.msg.type = this.radio
             console.log(this.jobVo)
             autoSendAdd(this.jobVo).then(response=>{
        if(response.msg=="操作成功")
        this.$message.success("定时发送设置成功");
        this.open = false;
        setTimeout(200)
        this.active = 0;
          this.userIds = null,
          this.getList()
          this.changeCon()
       

        })

            }
          }
          
     

          // console.log(this.jobVo)
          // if()
      
      },
      changeCon(){
        this.msg_title = null,
        this.msg_id = null
           this.value2 = null
           this.form2.type = null,
            this.form2.title = null,
            this.form2.content = null
            
      },
         // 选择框通过内容查找全部
         sellectTotal(val){
        // console.log("------------")
      this.msg_id =  this.options.find(md => md.value === val).id;
      this.msg_title = this.options.find(md => md.value === val).label;
      // console.log( this.msg_id)
      // // console.log(this.options)
      // console.log(this.msg_title)
      // console.log("------------")
    },
        /** 查询邮件模板列表 */
  getListModel() {
      this.loading = true;
      listModel(this.queryParams_model).then(response => {
        this.modelList = response.rows;
        // var cardNumObj = {};
          this.options.length = 0;

        this.modelList.forEach(model =>{
          //定义一个空的对象
          var  cardNumObj = { id: model.id, value: model.content,label:model.title };
          this.options.push(cardNumObj)
        })
        this.total = response.total;
        this.loading = false;
      });
    },
      getUserIds(val){
        // console.log(val)
        this.userIds = val[0];
      },
           // 步骤条下一步的方法
           next() {
            if(this.active == 0){
              // console.log("有没有定时参数?")
              if(this.form.jobName==null || this.form.expression==null || this.form.misfirePolicy==null
              || this.form.status==null || this.form.concurrent == null || this.form.cronExpression==null){
                this.$message.warning('请填写完整');
                   return
              }
            }
           
            if(this.active == 1){

              if(this.userIds == null || this.userIds.length == 0){
                this.$message.warning('请选择用户');
                   return
              }
               
            }
          if (this.active++ > 3) this.active = 0
        },
         // 步骤条上一步的方法
        pre() {
          if (this.active-- < 2) this.active = 0
        },
          /** 提交按钮 */
    submitTaskForm() {
      this.$refs["taskform"].validate(valid => {
        if (valid) {
          if (this.taskform.id != null) {
            updateTask(this.taskform).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openTask = false;
              this.getList();
            });
          } else {
            addTask(this.taskform).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openTask = false;
              this.getList();
            });
          }
        }
      });
    },
       // 取消按钮
       cancelTask() {
      this.openTask = false;
      this.resetTask();
    },
          // 表单重置
    resetTask() {
      this.taskform = {
        id: null,
        title: null,
        content: null,
        modelId: null,
        userId: null,
        jobId: null
      };
      this.resetForm("form");
    },
          /** 删除按钮操作 */
    handleDeleteTask(row) {
      const ids = row.id ;
      this.$modal.confirm('是否确认删除定时子任务编号为"' + ids + '"的数据项？').then(function() {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
          /** 新增按钮操作 */
    handleAddTask() {
      this.resetTask();
      this.openTask = true;
      this.tasktitle = "添加定时子任务";
    },
    /** 修改按钮操作 */
    handleUpdateTask(row) {
      this.resetTask();
      const id = row.id
      getTask(id).then(response => {
        this.taskform = response.data;
        this.openTask = true;
        this.tasktitle = "修改定时子任务";
      });
    },
      /** 查询定时任务列表 */
      getList() {
        this.loading = true;
        listJobByUUID(this.queryParams).then(response => {
          this.jobList = response.rows;
          console.log("-------------任务详情-------------")
          console.log(this.jobList)
          console.log("----------------------------------")
          this.total = response.total;
          this.$emit('currentCount',this.jobList.length)
          this.loading = false;
        });
        this.countRun();
      },
      // 任务组名字典翻译
      jobGroupFormat(row, column) {
        return this.selectDictLabel(this.dict.type.sys_job_group, row.jobGroup);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          jobId: undefined,
          jobName: undefined,
          jobGroup: undefined,
          invokeTarget: undefined,
          cronExpression: undefined,
          misfirePolicy: 1,
          concurrent: 1,
          status: "0"
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.jobId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      handleAddTaskRow(row) {
        // console.log("9512345676")
       
        
        // console.log("9512345676")
      this.resetTask();
      // this.taskform = row.tasks
        console.log(row.jobId)
        this.taskform.jobId = row.jobId
      this.openTask = true;
      this.tasktitle = "添加定时子任务";
    },
      // 更多操作触发
      handleCommand(command, row) {
        switch (command) {
          case "addTask":
          console.log(row)
            this.handleAddTaskRow(row.tasks[0]);
            break;
          case "handleRun":
            this.handleRun(row);
            break;
          case "handleView":
            this.handleView(row);
            break;
          case "handleJobLog":
            this.handleJobLog(row);
            break;
          default:
            break;
        }
      },
      // 任务状态修改
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.jobName + '"任务吗？').then(function() {
          return changeJobStatus(row.jobId, row.status);
        }).then(() => {
          this.countRun();
          this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      /* 立即执行一次 */
      handleRun(row) {
        this.$modal.confirm('确认要立即执行一次"' + row.jobName + '"任务吗？').then(function() {
          return runJob(row.jobId, row.jobGroup);
        }).then(() => {
          this.$modal.msgSuccess("执行成功");
        }).catch(() => {});
      },
      /** 任务详细信息 */
      handleView(row) {
        getJob(row.jobId).then(response => {
          this.form = response.data;
          this.openView = true;
        });
      },
      /** cron表达式按钮操作 */
      handleShowCron() {
        this.expression = this.form.cronExpression;
        this.openCron = true;
      },
      /** 确定后回传值 */
      crontabFill(value) {
        this.form.cronExpression = value;
      },
      /** 任务日志列表查询 */
      handleJobLog(row) {
        const jobId = row.jobId || 0;
        this.$router.push('/monitor/job-log/index/' + jobId)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const jobId = row.jobId || this.ids;
        getJob(jobId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改任务";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.jobId != undefined) {
              updateJob(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJob(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const jobIds = row.jobId || this.ids;
        this.$modal.confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项？').then(function() {
          return delJob(jobIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('monitor/job/export', {
          ...this.queryParams
        }, `job_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
<style lang="scss" scoped>
::v-deep .el-radio--medium.is-bordered {
  padding: 10px 20px 0 10px;
  border-radius: 4px;
  height: 36px;
  width: 45%;
  text-align: center;
}

</style>
  